 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.Conta;
import controller.ContaCorrente;
import controller.ContaInvestimento;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
import model.ContaDAO;

/**
 *
 * @author Carol
 */
public class frm_saque extends javax.swing.JFrame {

    /**
     * Creates new form frm_saque
     */
    public Conta conta;
    public int tipoConta;
    public DecimalFormat df = new DecimalFormat("0.00");
    
    public frm_saque() {
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle("Banco Internacional de TADS - Saque");
    }
    
    public frm_saque(Conta conta, int tipoConta){
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle("Banco Internacional de TADS - Saque");
        this.conta = conta;
        this.tipoConta = tipoConta;
        lblNumConta.setText(Integer.toString(conta.getNconta()));
        if(tipoConta == 1){
            ContaCorrente cc = (ContaCorrente) conta;
            lblValorMontMin.setText("-");
            lblValorLimite.setText(df.format(cc.getLimite() + cc.getSaldo()));
        }else{
            ContaInvestimento ci  = (ContaInvestimento) conta;
            lblValorMontMin.setText(df.format(ci.getMontmin()));
            lblValorLimite.setText(df.format(ci.getSaldo()));
        }
    
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        lblContaNo = new javax.swing.JLabel();
        lblNumConta = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblMontanteMin = new javax.swing.JLabel();
        lblLimite = new javax.swing.JLabel();
        lblDigiteValorSaque = new javax.swing.JLabel();
        lblValorMontMin = new javax.swing.JLabel();
        lblValorLimite = new javax.swing.JLabel();
        txtValorSaque = new javax.swing.JTextField();
        cmdSacar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        lblContaNo.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        lblContaNo.setText("Conta nº");

        lblNumConta.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        lblNumConta.setText("X");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblContaNo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblNumConta)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblContaNo)
                    .addComponent(lblNumConta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1)
                .addContainerGap())
        );

        lblMontanteMin.setText("Montante Mínimo:");

        lblLimite.setText("Saldo Total");

        lblDigiteValorSaque.setText("Digite o valor do saque:");

        lblValorMontMin.setText("XX,XX");

        lblValorLimite.setText("XX,XX");

        cmdSacar.setText("Sacar");
        cmdSacar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cmdSacarMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblDigiteValorSaque)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtValorSaque, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblLimite, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMontanteMin, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblValorMontMin, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblValorLimite, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(48, 48, 48))))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(cmdSacar)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMontanteMin)
                    .addComponent(lblValorMontMin))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblLimite)
                    .addComponent(lblValorLimite))
                .addGap(15, 15, 15)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDigiteValorSaque)
                    .addComponent(txtValorSaque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(cmdSacar)
                .addGap(0, 25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdSacarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdSacarMouseReleased
        if(tipoConta == 1){
            ContaCorrente cc = (ContaCorrente) conta;
            try {
                boolean resposta = cc.saca(Double.parseDouble(txtValorSaque.getText()));
                if(resposta){
                    JOptionPane.showMessageDialog(null, "Saque efetuado!","Operação Concluída", JOptionPane.INFORMATION_MESSAGE); 
                    frm_saldo tela = new frm_saldo(conta.getNconta());
                    tela.show();
                    this.dispose();                    
                }
            } catch (Exception ex) {
                Logger.getLogger(frm_saque.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            ContaInvestimento ci = (ContaInvestimento) conta;
            try {
                boolean resposta = ci.saca(Double.parseDouble(txtValorSaque.getText()));
                if(resposta){
                    JOptionPane.showMessageDialog(null, "Saque efetuado!","Operação Concluída", JOptionPane.INFORMATION_MESSAGE); 
                    frm_saldo tela = new frm_saldo(conta.getNconta());
                    tela.show();
                    this.dispose();
                }
            } catch (Exception ex) {
                Logger.getLogger(frm_saque.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_cmdSacarMouseReleased

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        frm_saldo tela = new frm_saldo(conta.getNconta());
        tela.show();
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

    }//GEN-LAST:event_formWindowClosed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frm_saque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frm_saque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frm_saque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frm_saque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frm_saque().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdSacar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblContaNo;
    private javax.swing.JLabel lblDigiteValorSaque;
    private javax.swing.JLabel lblLimite;
    private javax.swing.JLabel lblMontanteMin;
    private javax.swing.JLabel lblNumConta;
    private javax.swing.JLabel lblValorLimite;
    private javax.swing.JLabel lblValorMontMin;
    private javax.swing.JTextField txtValorSaque;
    // End of variables declaration//GEN-END:variables
}
